package com.qingneng.budget.web;

import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.qingneng.budget.service.sdk.IWorkBudgetService;
import com.qingneng.budget.util.BaseAction;
import com.qingneng.budget.util.UtilTool;
import com.qingneng.budget.vo.BudgetWorkDoInfo;
import com.qingneng.budget.vo.FeeItem;

public class CalFeeItemAction extends BaseAction {
	
	public static final Logger logger  = Logger.getLogger(CalFeeItemAction.class);
	
	public int project_workinfo_id;
	
	public List feeItemList ;
	
	
	public List getFeeItemList() {
		return feeItemList;
	}
	public void setFeeItemList(List feeItemList) {
		this.feeItemList = feeItemList;
	}
	public int getProject_workinfo_id() {
		return project_workinfo_id;
	}
	public void setProject_workinfo_id(int project_workinfo_id) {
		this.project_workinfo_id = project_workinfo_id;
	}

	public IWorkBudgetService workBudgetService;
	
	public CalFeeItemAction(){
		
	}
	public CalFeeItemAction(IWorkBudgetService workBudgetService){
		this.workBudgetService = workBudgetService;
	}

	
	
	private double totalAmount;
	
	private double fee_amount;
	
	private double item_rate ;
	
	double D4_fee_amount=0.0d;
	
	double D5_fee_amount=0.0d;
	
	double D6_fee_amount=0.0d;	
	
	double D7_fee_amount=0.0d;
	
	public String execute(){
		BudgetWorkDoInfo bean  = new BudgetWorkDoInfo();
		bean.setProject_workinfo_id(project_workinfo_id);
		//获取合计金额
		bean.setItem_id(23);
		List list  =  workBudgetService.getWorkDoInfo(bean);
		if(list!=null && list.size()>0){
			BudgetWorkDoInfo tmp = (BudgetWorkDoInfo)list.get(0);
			totalAmount =  tmp.getTotalAmount();
		}
		
		//获取取费项
		 feeItemList = workBudgetService.getFeeItem();
		if(feeItemList!=null && feeItemList.size()>0){
			for(int i=0;i<feeItemList.size();i++){
				FeeItem item = (FeeItem)feeItemList.get(i);
				
				if(item.getItem_id()==4 && "zjfxj".equals(item.getFee_item_name())){
					//取前三项的fee_amount
				
					for(int k=0;k<3;k++){
						FeeItem reitem = (FeeItem)feeItemList.get(k);
						double d_rate = new Double(reitem.getItem_rate())/100;
						D4_fee_amount  = D4_fee_amount+totalAmount*d_rate;
					}
					item.setFee_amount(UtilTool.doubleFormat(D4_fee_amount));
				}
				//算税金
				else if(item.getItem_id()==6 && "yj".equals(item.getFee_item_name())){
					
					double yj_rate = new Double(item.getItem_rate())/100;
					//取前三项的fee_amount
					double amount =0d;
					for(int k=3;k<5;k++){
						FeeItem reitem = (FeeItem)feeItemList.get(k);
						double d_rate = new Double(reitem.getItem_rate())/100;
						if(k==3){
							D4_fee_amount = D4_fee_amount*d_rate;
						}else{
							D5_fee_amount = totalAmount*d_rate;
						}
						
					}
					D6_fee_amount  = (D4_fee_amount+D5_fee_amount)*yj_rate;
					item.setFee_amount(UtilTool.doubleFormat(D6_fee_amount*yj_rate));
				}
				
				//合计计算
				else if(item.getItem_id()==7 && "budget_amount".equals(item.getFee_item_name())){
					double total_rate = new Double(item.getItem_rate())/100;
					D7_fee_amount = D4_fee_amount+D5_fee_amount+D6_fee_amount;
					item.setFee_amount(UtilTool.doubleFormat(D7_fee_amount*total_rate));
				}
				else {
					fee_amount = totalAmount * new Double (item.getItem_rate())/100;
					item.setFee_amount(UtilTool.doubleFormat(fee_amount));
				}
			}
		}
		
		
		return Action.SUCCESS;
	}
	
/*	public static void main(String args[]){
		String formual = "$arg$*0.75";	
		Map<String, Object> vars = new HashMap<String, Object>();   
        vars.put("$arg$", new Integer(100));   
        Double result = (Double )MVEL.eval(formual, vars);  		
		//String replacement = "111";
		//String exe =  formual.replaceAll("\\$arg\\$",replacement);
		System.out.print(result);
	}*/

	

}
